package com.midea.ai.air.ui.more;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.ActionBarBackFragment;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.common.ConsVal;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.utils.ComUtils;

/**
 * Created by ouyangdi on 2016/4/15.
 * 修改地址Activity
 */
public class MorePersonAddChangeActivity extends BaseActivity {
    ActionBarBackFragment mActionBar;
    private TextView mActionBarRightBtn;
    private String mActionBarTitle;

    private EditText mInputName;
    private ImageView mDeleteName;

    private EditText mInputContact;
    private ImageView mDeleteContact;

    private EditText mInputProvince;
    private ImageView mDeleteProvince;

    private EditText mInputDetail;
    private ImageView mDeleteDetail;

    private EditText mInputPostcode;
    private ImageView mDeletePostcode;
    private View.OnClickListener mOnClickListener;
    private TextWatcher mTextWatcher;
    private View.OnFocusChangeListener mOnFocusChangeListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_more_person_add_change);

        mActionBarTitle = getIntent().getStringExtra(ConsVal.KEY_TITLE);
        initListener();
        initView();
    }

    private void initListener() {
        mOnClickListener = new View.OnClickListener() {
            private EditText mEditText;
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.delete_add_name:
                        mEditText = mInputName;
                        break;
                    case R.id.delete_add_contact:
                        mEditText = mInputContact;
                        break;
                    case R.id.delete_add_province:
                        mEditText = mInputProvince;
                        break;
                    case R.id.delete_add_detail:
                        mEditText = mInputDetail;
                        break;
                    case R.id.delete_add_postcode:
                        mEditText = mInputPostcode;
                        break;
                }
                mEditText.setText("");
            }
        };

        mTextWatcher = new TextWatcher() {
            private ImageView mDelete;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                View view= getCurrentFocus();
                if(view != null) {
                    switch (view.getId()) {
                        case R.id.edit_add_name:
                            mDelete = mDeleteName;
                            break;
                        case R.id.edit_add_contact:
                            mDelete = mDeleteContact;
                            break;
                        case R.id.edit_add_province:
                            mDelete = mDeleteProvince;
                            break;
                        case R.id.edit_add_detail:
                            mDelete = mDeleteDetail;
                            break;
                        case R.id.edit_add_postcode:
                            mDelete = mDeletePostcode;
                            break;
                    }
                }
                if(TextUtils.isEmpty(s)) {
                    if(isAllEditEmpty()) {
                        mActionBarRightBtn.setAlpha(0.25f);
                    }
                    mDelete.setVisibility(View.GONE);
                    return;
                }
                mActionBarRightBtn.setAlpha(1f);
                mDelete.setVisibility(View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        mOnFocusChangeListener = new View.OnFocusChangeListener() {
            private ImageView mDelete;
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                switch (v.getId()) {
                    case R.id.edit_add_name:
                        mDelete = mDeleteName;
                        break;
                    case R.id.edit_add_contact:
                        mDelete = mDeleteContact;
                        break;
                    case R.id.edit_add_province:
                        mDelete = mDeleteProvince;
                        break;
                    case R.id.edit_add_detail:
                        mDelete = mDeleteDetail;
                        break;
                    case R.id.edit_add_postcode:
                        mDelete = mDeletePostcode;
                        break;
                }
                if(!hasFocus) {
                    mDelete.setVisibility(View.GONE);
                } else {
                    EditText editText = (EditText)v;
                    if(!TextUtils.isEmpty(editText.getText())) {
                        mDelete.setVisibility(View.VISIBLE);
                    }
                }
            }
        };
    }
    private void initView() {
        initActionBar();

        mInputName = (EditText) findViewById(R.id.edit_add_name);
        mInputName.addTextChangedListener(mTextWatcher);
        mInputName.setOnFocusChangeListener(mOnFocusChangeListener);

        mInputContact = (EditText) findViewById(R.id.edit_add_contact);
        mInputContact.addTextChangedListener(mTextWatcher);
        mInputContact.setOnFocusChangeListener(mOnFocusChangeListener);

        mInputProvince = (EditText) findViewById(R.id.edit_add_province);
        mInputProvince.addTextChangedListener(mTextWatcher);
        mInputProvince.setOnFocusChangeListener(mOnFocusChangeListener);

        mInputDetail = (EditText) findViewById(R.id.edit_add_detail);
        mInputDetail.addTextChangedListener(mTextWatcher);
        mInputDetail.setOnFocusChangeListener(mOnFocusChangeListener);

        mInputPostcode = (EditText) findViewById(R.id.edit_add_postcode);
        mInputPostcode.addTextChangedListener(mTextWatcher);
        mInputPostcode.setOnFocusChangeListener(mOnFocusChangeListener);

        mDeleteName = (ImageView) findViewById(R.id.delete_add_name);
        mDeleteContact = (ImageView) findViewById(R.id.delete_add_contact);
        mDeleteProvince = (ImageView) findViewById(R.id.delete_add_province);
        mDeleteDetail = (ImageView) findViewById(R.id.delete_add_detail);
        mDeletePostcode = (ImageView) findViewById(R.id.delete_add_postcode);

        mDeleteName.setOnClickListener(mOnClickListener);
        mDeleteContact.setOnClickListener(mOnClickListener);
        mDeleteProvince.setOnClickListener(mOnClickListener);
        mDeleteDetail.setOnClickListener(mOnClickListener);
        mDeletePostcode.setOnClickListener(mOnClickListener);
    }

    /**
     * 判断是否所有的输入框都为空
     * @return true:所有的输入框都为空 false:所有的输入框不都为空
     */
    private boolean isAllEditEmpty() {
        return (TextUtils.isEmpty(mInputName.getText()) && TextUtils.isEmpty(mInputContact.getText()) &&
                    TextUtils.isEmpty(mInputProvince.getText()) && TextUtils.isEmpty(mInputDetail.getText()) && TextUtils.isEmpty(mInputPostcode.getText()));
    }

    //点击EditText以外的区域隐藏键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (ComUtils.isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private void initActionBar() {
        FrameLayout mBarLayout = (FrameLayout)findViewById(R.id.bar_parent);
        ViewGroup.LayoutParams LP = mBarLayout.getLayoutParams();
        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
        mBarLayout.setLayoutParams(LP);
        mActionBar = (ActionBarBackFragment)getSupportFragmentManager().findFragmentById(R.id.bar_fragment);
        mActionBarRightBtn = mActionBar.getTvBarRight();
        mActionBarRightBtn.setText(getResources().getString(R.string.action_save));
        mActionBarRightBtn.setVisibility(View.VISIBLE);
        mActionBarRightBtn.setAlpha(0.25f);
        mActionBar.getBackButton().setVisibility(View.GONE);
        mActionBar.getTvBarLeft().setVisibility(View.VISIBLE);
        mActionBar.getTvTitle().setText(mActionBarTitle);
    }
}
